#define _CRT_SECURE_NO_WARNINGS 1

class H2O {
private:
    int _h;
    int _o;
    pthread_cond_t _cond;
    pthread_mutex_t _mutex;
public:
    H2O() :_h(0), _o(0) {
        pthread_mutex_init(&_mutex, nullptr);
        pthread_cond_init(&_cond, nullptr);
    }

    void hydrogen(function<void()> releaseHydrogen) {
        pthread_mutex_lock(&_mutex);
        while (_h == 2)
        {
            pthread_cond_wait(&_cond, &_mutex);
        }
        // releaseHydrogen() outputs "H". Do not change or remove this line.
        ++_h;
        releaseHydrogen();
        if (_h == 2 && _o == 1)
        {
            _h = 0; _o = 0;
            pthread_cond_broadcast(&_cond);
        }
        else
            pthread_cond_signal(&_cond);
        pthread_mutex_unlock(&_mutex);
    }

    void oxygen(function<void()> releaseOxygen) {
        pthread_mutex_lock(&_mutex);
        while (_o == 1)
        {
            pthread_cond_wait(&_cond, &_mutex);
        }
        ++_o;
        // releaseHydrogen() outputs "H". Do not change or remove this line.
        releaseOxygen();
        if (_h == 2 && _o == 1)
        {
            _h = 0; _o = 0;
            pthread_cond_broadcast(&_cond);
        }
        else
            pthread_cond_signal(&_cond);
        pthread_mutex_unlock(&_mutex);
    }
};